<template>
  <div class="permission-container">
    <div class="app-container">
      <PageTools>
        <template #right><el-button type="primary" size="small" @click="show(1, '0')">添加权限</el-button> </template>
      </PageTools>
      <el-card>
        <el-table border stripe :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />

          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type === 1" type="primary" size="mini" @click="show(2, row.id)">添加</el-button>
              <el-button type="warning" size="mini" @click="edit(row.id)">编辑</el-button>
              <el-button type="danger" size="mini" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 新增编辑的弹层 -->
        <el-dialog :visible="showDialog" title="弹层标题" @close="close">
          <!-- 表单内容 -->
          <el-form label-width="100px">
            <el-form-item label="权限名称">
              <el-input v-model="formData.name" />
            </el-form-item>
            <el-form-item label="权限标识">
              <el-input v-model="formData.code" />
            </el-form-item>
            <el-form-item label="权限描述">
              <el-input v-model="formData.description" />
            </el-form-item>
            <el-form-item label="权限启用">
              <el-switch
                v-model="formData.enVisible"
                active-text="启用"
                active-value="1"
                inactive-text="不启用"
                inactive-value="0"
              />
            </el-form-item>
          </el-form>

          <template #footer>
            <div style="text-align: right;">
              <el-button @click="close">取消</el-button>
              <el-button type="primary" @click="handleSubmit">确定</el-button>
            </div>
          </template>
        </el-dialog>
      </el-card>
    </div>

  </div>
</template>

<script>
import { reqGetPermissionList, reqAddPermission, reqDelPermission, reqGetPermissionDetail, reqUpdatePermission } from '@/api/permission'
import { tranlateToData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [], // 权限列表数据
      showDialog: false,
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取所有权限列表
    async getPermissionList() {
      const { data } = await reqGetPermissionList()
      this.list = tranlateToData(data, '0')
    },
    show(type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 关闭并重置
    close() {
      this.showDialog = false
      this.formData = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 确定按钮处理函数
    async handleSubmit() {
      // 1. 预校验省略
      if (this.formData.id) {
        // 编辑提交请求
        await reqUpdatePermission(this.formData)
      } else {
        // 2. 发送新增请求
        await reqAddPermission(this.formData)
      }
      // 3. 关闭对话框+重置表单
      this.close()
      // 4.重新获取
      this.getPermissionList()
    },
    // 删除
    async del(id) {
      // 1. 确认消息提示
      const res = await this.$confirm('此操作将永久删除该权限点, 是否继续?', '提示', {
        type: 'warning'
      }).catch(() => {})
      if (res !== 'confirm') return this.$message.info('已取消')
      // 2. 携带id发送请求
      await reqDelPermission(id)
      // 3. 重新获取
      this.getPermissionList()
    },
    async edit(id) {
      // 展示对话框
      this.showDialog = true
      // 查询权限点信息
      const { data } = await reqGetPermissionDetail(id)
      this.formData = data
    }
  }
}
</script>

<style>

</style>
